﻿#ifndef CIECHROMATICITYPLOT_H
#define CIECHROMATICITYPLOT_H

#include <QWidget>
#include <QVector>
#include <QPolygonF>
#include <QImage>

class QCustomPlot;
class QCPCurve;
class QCPItemText;
class QCPItemTracer;
class QCPGraph;

struct CMFRow {
    double wl;
    double xbar, ybar, zbar;
};

enum class DiagramKind {
    CIE1931_xy,
    CIE1976_uv,
    CIE2015_uv
};

class CIEChromaticityPlot : public QWidget
{
    Q_OBJECT
public:
    explicit CIEChromaticityPlot(QWidget *parent = nullptr);

    // 加载 CMF（CSV: wavelength,xbar,ybar,zbar；允许表头）
    bool loadCMF(DiagramKind kind, const QString &csvPath);

    // 切换图型：1931/1976/2015（2015 使用 2015 CMF，投影为 u′v′）
    void setDiagram(DiagramKind kind);

    // 叠加/清理
    bool setSpectrum(const QVector<double> &wavelength_nm,
                     const QVector<double> &power);
    void addPoint_xy(double x, double y, const QString &label = QString());
    void clearPoints();
    void clearSpectrumPoint();

    // 覆盖物开关
    void setShowSRGB(bool on);
    void setShowAdobeRGB(bool on);
    void setShowPlanck(bool on);
    void setShowIsoCCT(bool on);
    void setShowDominantLines(bool on);

signals:
    void cmfLoaded(DiagramKind kind, bool ok);

protected:
    void resizeEvent(QResizeEvent *e) override;

private:
    // 基础
    void setupPlot();
    void rebuildAll();
    void buildSpectralLocus();
    void updateChromaticityBackground();
    void buildPlanckAndIsoCCT();
    void buildGamutTriangles();
    void buildDominantWavelengthLines();

    // 新增：波长标注 + 自动取景（u′v′）
    void annotateWavelengths();
    void clearWavelengthAnnotations();
    void autoRangeToLocus();

    // 颜色学/投影
    static inline bool xyz2xy(double X, double Y, double Z, double &x, double &y);
    static inline void xyz2uvprime(double X, double Y, double Z, double &up, double &vp);
    static inline bool uvp2xy(double up, double vp, double &x, double &y);
    bool cmfAt(const QVector<CMFRow>& cmf, double wl, double &xbar, double &ybar, double &zbar) const;
    bool integrateSPD(const QVector<CMFRow>& cmf,
                      const QVector<double> &wavelength_nm,
                      const QVector<double> &power,
                      double &X, double &Y, double &Z) const;
    static double blackbodyRel(double wl_nm, double T_K);

    QPointF projectFromXYZ(double X, double Y, double Z) const;
    QPointF projectFromxy(double x, double y) const;
    bool    xyFromCurrentAxes(double ax, double ay, double &x, double &y) const;

    static inline QColor sRGBfromxyY(double x, double y, double Y=1.0);

private:
    // UI
    QCustomPlot   *plot_ = nullptr;

    // 基础曲线
    QCPCurve      *locusCurve_    = nullptr;
    QCPCurve      *purpleEdge_    = nullptr;

    // 覆盖物
    QCPCurve      *planckCurve_   = nullptr;
    QCPGraph      *isoCCTGraph_   = nullptr;
    QCPCurve      *srgbCurve_     = nullptr;
    QCPCurve      *adobeCurve_    = nullptr;
    QCPGraph      *domWLGraph_    = nullptr;

    // CMF
    QVector<CMFRow> cmf1931_;
    QVector<CMFRow> cmf2015_;
    DiagramKind     kind_ = DiagramKind::CIE1931_xy;

    // 背景与马蹄多边形（当前图坐标）
    QPolygonF locusPoly_;
    QImage    bgImage_;

    // 光谱点与自定义点
    QCPItemTracer *spectrumTracer_  = nullptr;
    QCPItemText   *spectrumLabel_   = nullptr;
    struct MarkItem { QCPItemTracer *tracer; QCPItemText *label; };
    QVector<MarkItem> markers_;

    // 覆盖物开关
    bool showSRGB_  = true;
    bool showAdobe_ = true;
    bool showPlanck_= true;
    bool showIsoCCT_= true;
    bool showDomWL_ = true;

    // 波长标注
    QVector<QCPItemText*>   wlLabels_;
    QVector<QCPItemTracer*> wlDots_;
};

#endif // CIECHROMATICITYPLOT_H
